﻿'*************************************************************************
'@Tác giả : Dương Văn Thỉnh
'@Tên file: FrmPhieuThuChi.vb
'@Ngày tạo: 17/06/2010
'@Ngày sửa: 22/06/2010
'@Mục đích: Hiển thị phiếu thu chi.
 
'*************************************************************************
'vFrmPhieuThu	Phiếu thu
'eFrmPhieuThu	Phieu thu
'vFrmPhieuChi	Phiếu chi
'eFrmPhieuChi	Phieu chi
'-------------------------------------------------------------------------
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository
Imports System.Data
Imports System.Data.Linq
Imports System.Linq
Imports System.Windows.Forms
Imports Publics
Imports Publics.DataMainDataContext
Imports System.ComponentModel


Public Class FrmPhieuThuST
    Public blnLaChonHoaDon As Boolean

    Private _blnLaPhieuThu As Boolean
    Private _LaPhieuBanHang As Boolean

    Private _LoaiPhieu As Integer
    Public Property LoaiPhieu() As Integer
        Get
            Return _LoaiPhieu
        End Get
        Set(ByVal value As Integer)
            _LoaiPhieu = value
        End Set
    End Property

    Public Property LaPhieuBanHang() As Boolean
        Get
            Return _LaPhieuBanHang
        End Get
        Set(ByVal value As Boolean)
            _LaPhieuBanHang = value
        End Set
    End Property

    Private _TuNgay As DateTime
    Public Property TuNgay() As DateTime
        Get
            Return _TuNgay
        End Get
        Set(ByVal value As DateTime)
            _TuNgay = value
        End Set
    End Property

    Private _DenNgay As DateTime
    Public Property DenNgay() As DateTime
        Get
            Return _DenNgay
        End Get
        Set(ByVal value As DateTime)
            _DenNgay = value
        End Set
    End Property


    Private _ThangLamViec As Integer
    Public Property Thang() As Integer
        Get
            Return _ThangLamViec
        End Get
        Set(ByVal value As Integer)
            _ThangLamViec = value
        End Set
    End Property


    Private _Nam As Integer
    Public Property Nam() As Integer
        Get
            Return _Nam
        End Get
        Set(ByVal value As Integer)
            _Nam = value
        End Set
    End Property

    ''' <summary>
    ''' Điền dữ liệu vào Grid
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub FillToGrid()
        Using db As New DataMainDataContext(ConnectionString)
            Dim lst = (From ds In db.ST_PhieuThus _
                                     Where ds.InUsed And ds.NgayCT.Month = Thang _
                                     And ds.NgayCT.Year = Nam And ds.LoaiPhieu = LoaiPhieu _
                                     Select New With {.ID = ds.ID, .NgayCT = Format(ds.NgayCT, "dd/MM/yyyy"), .IDKhachHang_NCC = ds.IDKhachHang_NCC, _
                                    .NguoiNop = ds.NguoiNop, .LyDoNop = ds.LyDoNop, .DiaChi = ds.DiaChi, .SoPhieu = ds.SoPhieu, .SoTien = ds.SoTien, .TenDonVi = ""}).ToList
            For Each item In From ds In lst Join kh In lstSourceAllKhachHang On ds.IDKhachHang_NCC Equals kh.ID _
                             Select ds, kh
                item.ds.TenDonVi = item.kh.TenKhachHang
            Next
            GrdChungTu.DataSource = lst
        End Using
    End Sub
    ''' <summary>
    ''' Đóng
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CloseIt()
        Me.Close()
    End Sub
    ''' <summary>
    ''' Thêm mới phiếu thu chi
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatPhieuThuST
        frm.ActionForm = vAddAction
        frm.LoaiPhieu = LoaiPhieu
        frm.Thang = Thang
        frm.Nam = Nam
        frm.Refresh = New FrmCapNhatPhieuThuST.Fill(AddressOf FillToGrid)
        frm.ShowDialog()
        FillToGrid()
        If Not String.IsNullOrEmpty(frm.ID) Then
            For i = 0 To grvChungTu.RowCount - 1
                If frm.ID = CStr(grvChungTu.GetRowCellValue(i, "ID")) Then
                    grvChungTu.FocusedRowHandle = i
                End If
            Next
        End If
        If frm.LaInPhieu Then
            InPhieuThuChi(frm.ID)
        End If
    End Sub

    ''' <summary>
    ''' Hiển thị thông tin phím tắt trên caption của form
    ''' @NhiLV
    ''' </summary>
    ''' <remarks></remarks>
    Private Function GetCaptionForm() As String
        Dim strThem As String = String.Empty
        Dim strSua As String = String.Empty
        Dim strXoa As String = String.Empty
        Dim strXuat As String = String.Empty
        Dim strTimKiem As String = String.Empty
        'Dim strLamTuoi As String = String.Empty
        'Dim strTroGiup As String = String.Empty
        Dim strThoat As String = GetStrKey(kThoat)

        If btnThemMoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnThemMoi.Enabled = True Then
            strThem = GetStrKey(kThem)
        End If
        If btnSuaDoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnSuaDoi.Enabled = True Then
            strSua = GetStrKey(kSua)
        End If
        If btnXoa.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnXoa.Enabled = True Then
            strXoa = GetStrKey(kXoa)
        End If
        If btnExcel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnExcel.Enabled = True Then
            strXuat = GetStrKey(kXuat)
        End If

        'If btnTroGiup.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnTroGiup.Enabled = True Then
        '    strTroGiup = GetStrKey(kTroGiup)
        'End If
        'If btnLamTuoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnLamTuoi.Enabled = True Then
        '    strLamTuoi = GetStrKey(kLamTuoi)
        'End If
        Return strThem + strSua + strXoa + strXuat + strTimKiem + strThoat 'strLamTuoi + strTroGiup +
    End Function


    ''' <summary>
    ''' Load Form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub FrmHoaDonNhap_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DenNgay = DateSerial(vIntKTNam, vIntKTThang, Date.DaysInMonth(vIntKTNam, vIntKTThang))
        TuNgay = DateSerial(vIntKTNam, vIntKTThang, 1)
        btnIn.Caption = BtnPrintCaption
        btnThemMoi.Caption = BtnAddNewCaption
        btnSuaDoi.Caption = BtnEditCaption
        btnXoa.Caption = BtnDeleteCaption
        btnLamTuoi.Caption = BtnRefreshCaption
        btnExcel.Caption = BtnExportDataCaption
        btnChapNhan.Caption = BtnAcceptCaption
        btnTroGiup.Caption = BtnHelpCaption
        btnIn.Caption = BtnPrintCaption
        btnHuybo.Caption = BtnCloseCaption

        If blnLaChonHoaDon Then
            btnThemMoi.Enabled = False
            btnSuaDoi.Enabled = False
            btnXoa.Enabled = False
            btnChapNhan.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
        End If
        Me.Text = String.Format("{0} Tháng {1} Năm {2}", Text, Thang, Nam)
        FillToGrid()
        'Dim objkhoaso = (From ds In dbVms.KT_DM_KhoaSos _
        '                 Where ds.InUsed And ds.Thang = Thang And ds.Nam = Nam _
        '                 And ds.DaKhoaSo.GetValueOrDefault = True _
        '                 Select ds).FirstOrDefault
        'If objkhoaso IsNot Nothing Then
        '    btnThemMoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        '    btnSuaDoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        '    btnXoa.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        '    btnCopy.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        'End If

    End Sub
    ''' <summary>
    ''' Sửa phiếu thu chi
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaDoi.ItemClick
        Dim id = CStr(grvChungTu.GetRowCellValue(grvChungTu.FocusedRowHandle, "ID"))
        Using db As New DataMainDataContext(ConnectionString)
            Dim obj = (From ds In db.ST_PhieuThus Where ds.ID = id Select ds).FirstOrDefault
            If obj IsNot Nothing Then
                Dim frm As New FrmCapNhatPhieuThuST
                frm.ActionForm = vEditAction
                frm.LoaiPhieu = LoaiPhieu
                frm.Thang = Thang
                frm.Nam = Nam
                frm.PhieuThu = obj
                frm.Refresh = New FrmCapNhatPhieuThuST.Fill(AddressOf FillToGrid)
                frm.ShowDialog()
                FillToGrid()
                If Not String.IsNullOrEmpty(frm.ID) Then
                    For i = 0 To grvChungTu.RowCount - 1
                        If frm.ID = CStr(grvChungTu.GetRowCellValue(i, "ID")) Then
                            grvChungTu.FocusedRowHandle = i
                        End If
                    Next
                End If
                If frm.LaInPhieu Then
                    InPhieuThuChi(obj.ID)
                End If

            End If
        End Using
    End Sub
    ''' <summary>
    ''' Xóa phiếu thu
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        Using db As New DataMainDataContext(ConnectionString)
            If MsgAskDelete("Bạn có muốn xóa chứng từ này không?") = vMsg.vReSult.Yes Then
                Dim id = CStr(grvChungTu.GetRowCellValue(grvChungTu.FocusedRowHandle, "ID"))
                Dim obj = (From ds In db.ST_PhieuThus Where ds.ID = id Select ds).FirstOrDefault
                If obj IsNot Nothing Then
                    obj.InUsed = False
                    obj.EditedOn = db.GetDateTimeServer
                    db.SubmitChanges()
                    FillToGrid()
                End If
            End If
        End Using
    End Sub
    ''' <summary>
    ''' Xuất dữ liệu
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnExcel.ItemClick
        'SaveLog_KeToan("Danh sách chứng từ phiếu thu siêu thị", "Xuất dữ liệu", "Xuất dữ liệu danh sách chứng từ phiếu thu siêu thị")
        Export_Dialog(SaveFileDialog1, GrdChungTu)
    End Sub
    ''' <summary>
    ''' Làm tươi
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLamTuoi.ItemClick
        FillToGrid()
    End Sub
    ''' <summary>
    ''' Đóng
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnHuybo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnHuybo.ItemClick
        Me.Close()
    End Sub

    Private Sub FrmPhieuThuChi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            BtnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSuaDoi) Then
            btnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xoá
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Làm tươi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH) Then
            btnLamTuoi_ItemClick(Nothing, Nothing)
        End If

        'Trợ giúp
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pINFO) Then
            btnTroGiup_ItemClick(Nothing, Nothing)
        End If
        'Đóng
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXIT) Then
            btnHuybo_ItemClick(Nothing, Nothing)
        End If

    End Sub

    ''' <summary>
    ''' Trợ giúp
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTroGiup.ItemClick
        'MsgErr(GetTextFromResx("MsgChuaCoThongTinTroGiup"))
    End Sub

    ''' <summary>
    ''' In phiếu thu chi   sua
    ''' </summary>
    ''' <param name="sID"></param>
    ''' <remarks></remarks>
    Public Sub InPhieuThuChi(ByVal sID As String)
        Dim pathReportPhatHanh As String = ""
        Dim rpt As New rptPhieuThuChiSieuThi
        Dim strPath As String = pathReportPhatHanh & "\\" & "rptPhieuThuChiSieuThi.repx"
        Try
            rpt = DevExpress.XtraReports.UI.XtraReport.FromFile(strPath, True)
        Catch ex As Exception
            rpt = New rptPhieuThuChiSieuThi
        End Try
        rpt.PhieuThu = (From ds In dbTV.ST_PhieuThus Where ds.ID = sID Select ds).FirstOrDefault
        Dim frm As New FrmCustomReport(rpt, strPath, FrmCustomReport.PhieuThuSieuThi)
        frm.WindowState = FormWindowState.Maximized
        frm.ShowDialog()
    End Sub


    Private Sub grvChungTu_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = 39 Then
            grvChungTu.ExpandMasterRow(grvChungTu.FocusedRowHandle, GrdChungTu.LevelTree.Nodes(0).RelationName)
        End If
        If e.KeyCode = 37 Then
            grvChungTu.CollapseMasterRow(grvChungTu.FocusedRowHandle, GrdChungTu.LevelTree.Nodes(0).RelationName)
        End If
    End Sub




    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub btnCopy_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCopy.ItemClick
        Dim id = CStr(grvChungTu.GetRowCellValue(grvChungTu.FocusedRowHandle, "ID"))
        Using db As New DataMainDataContext(ConnectionString)
            Dim obj = (From ds In db.ST_PhieuThus Where ds.ID = id Select ds).FirstOrDefault
            If obj IsNot Nothing Then
                Dim frm As New FrmCapNhatPhieuThuST
                frm.ActionForm = vCopyAction
                frm.LoaiPhieu = LoaiPhieu
                frm.Thang = Thang
                frm.Nam = Nam
                frm.PhieuThu = obj
                frm.Refresh = New FrmCapNhatPhieuThuST.Fill(AddressOf FillToGrid)
                frm.ShowDialog()
                FillToGrid()
                If Not String.IsNullOrEmpty(frm.ID) Then
                    For i = 0 To grvChungTu.RowCount - 1
                        If frm.ID = CStr(grvChungTu.GetRowCellValue(i, "ID")) Then
                            grvChungTu.FocusedRowHandle = i
                        End If
                    Next
                End If
                If frm.LaInPhieu Then
                    InPhieuThuChi(frm.ID)
                End If

            End If
        End Using
    End Sub

    Private Sub btnXem_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXem.ItemClick
        Dim id = CStr(grvChungTu.GetRowCellValue(grvChungTu.FocusedRowHandle, "ID"))
        Using db As New DataMainDataContext(ConnectionString)
            Dim obj = (From ds In db.ST_PhieuThus Where ds.ID = id Select ds).FirstOrDefault
            If obj IsNot Nothing Then
                Dim frm As New FrmCapNhatPhieuThuST
                frm.LoaiPhieu = LoaiPhieu
                frm.ActionForm = vViewAction
                frm.Thang = Thang
                frm.Nam = Nam
                frm.PhieuThu = obj
                frm.Refresh = New FrmCapNhatPhieuThuST.Fill(AddressOf FillToGrid)
                frm.ShowDialog()
            End If
        End Using
    End Sub

    Private Sub btnIn_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnIn.ItemClick
        Dim id = CStr(grvChungTu.GetRowCellValue(grvChungTu.FocusedRowHandle, "ID"))
        InPhieuThuChi(id)
    End Sub
    Public Shared thempt, suapt, xoapt, xempt, inpt As Boolean
    Public Shared thempc, suapc, xoapc, xempc, inpc As Boolean
    Public Sub KTQuyen()
        'Dim objKhoaSo = (From ds In dbVms.KT_DM_KhoaSos Where ds.InUsed AndAlso ds.Thang = Thang AndAlso ds.Nam = Nam And ds.DaKhoaSo.GetValueOrDefault = True Select ds).FirstOrDefault
        'If objKhoaSo IsNot Nothing Then
        '    btnThemMoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        '    btnCopy.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        '    btnSuaDoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        '    btnXoa.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        '    Return
        'End If
        Select Case LoaiPhieu
            Case ELoaiChungTu.PhieuThu
                btnCopy.Visibility = IIf(thempt, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnThemMoi.Visibility = IIf(thempt, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnSuaDoi.Visibility = IIf(suapt, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnXoa.Visibility = IIf(xoapt, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnXem.Visibility = IIf(xempt, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnIn.Visibility = IIf(inpt, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)

            Case ELoaiChungTu.PhieuChi
                btnCopy.Visibility = IIf(thempc, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnThemMoi.Visibility = IIf(thempc, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnSuaDoi.Visibility = IIf(suapc, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnXoa.Visibility = IIf(xoapc, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnXem.Visibility = IIf(xempc, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
                btnIn.Visibility = IIf(inpc, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)

        End Select


    End Sub
End Class